package com.br.utils;

import java.io.IOException;

import org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;

public class OurClassLoader extends ClassLoader{
	
	
	private ClassPool pool;
	
	
	public OurClassLoader(String path) {
	
		path.replace('.', '/');
		
		pool = new ClassPool();
		try {
			pool.appendClassPath(path);
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}
	
	 public static Class getClassRightClick(String fullyName) {
		 
		 System.out.println("Aqui dentro");
		 
		 
		 
		 ClassPool cp = ClassPool.getDefault();
		 CtClass cc = null;
		 Class classRightClick = null;
		 try {
			cc = cp.get(fullyName);
			classRightClick = cc.toClass();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CannotCompileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		 //Digester
		 //System.out.println("Aqui fora");
		 return classRightClick;
		 
	 }
	 
	 
	 public Class findClass(String name) throws ClassNotFoundException {
	        try {
	            CtClass cc = pool.get(name);
	            // modify the CtClass object here
	            byte[] b = cc.toBytecode();
	            return defineClass(name, b, 0, b.length);
	        } catch (NotFoundException e) {
	            throw new ClassNotFoundException();
	        } catch (IOException e) {
	            throw new ClassNotFoundException();
	        } catch (CannotCompileException e) {
	            throw new ClassNotFoundException();
	        }
	    }

}
